package top.zhaoyuliu.logbook.client.appender;

import org.slf4j.impl.StaticLoggerBinder;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.Appender;

/**
 * Copyright © 2021 Arain. All rights reserved.
 *
 * @Title: AppenderInit.java
 * @Prject: logbook-client
 * @Package: top.zhaoyuliu.logbook.client.appender
 * @Description: <功能详细描述>
 * @author: Arain
 * @date: 2021年1月31日 下午9:07:07
 * @version: V1.0
 */

public class LogAppenderInit implements ApplicationRunner {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void run(ApplicationArguments args) throws Exception {
			try {
				LoggerContext lc = (LoggerContext) StaticLoggerBinder.getSingleton().getLoggerFactory();
				LbLogback ca = new LbLogback();
				ca.setContext(lc);
				ca.setName("LbAppender");
				ca.start();

				Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
				rootLogger.addAppender((Appender) ca);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
}